/**
 * 
 * @Project le cOeur - Data Virtualization
 * @Date 08.12.2012
 * @Author Aebi Pascal, Kocher Marc
 * @School BFH-TI Biel, Bachelor Thesis
 *
 */

package main.framework.security;

import java.util.ArrayList;
import java.util.List;

import main.framework.exception.SecurityAccessException;

/**
 * This abstract class is used as holder of hosts.
 * The detailed implementation is done by the sub classes.
 */
public abstract class AccessList {

	protected List<String> hosts;
	
	public AccessList() {
		this.hosts = new ArrayList<>();
	}
	
	/**
	 * Checks if the given host is permitted to access the server.
	 * The given host must be either an IP address or a domain name.
	 * 
	 * @param host The host.
	 * @throws SecurityAccessException If the access for the given host is denied.
	 */
	public abstract void checkAccessList(String host) throws SecurityAccessException;
	
	/**
	 * Adds the given host to the list.
	 * The given host must be either an IP address or a domain name.
	 * 
	 * @param host The host.
	 */
	public void addHost(String host) {
		if(!this.hosts.contains(host)){
			this.hosts.add(host);
		}
	}
	
	/**
	 * Removes the given host form the list.
	 * The given host must be either an IP address or a domain name.
	 * 
	 * @param host The host.
	 */
	public void removeHost(String host) {
		if(this.hosts.contains(host)){
			this.hosts.remove(host);
		}
	}
	
	/**
	 * Adds the given host list to the existing list.
	 * The hosts must be either an IP address or a domain name.
	 * 
	 * @param hosts The host list.
	 */
	public void addHosts(List<String> hosts) {
		for (String host : hosts) {
			if(!this.hosts.contains(host)){
				this.hosts.add(host);
			}
		}
	}
	
	/**
	 * Removes all hosts form the host list.
	 */
	public void removeHosts() {
		this.hosts.clear();
	}
	
	/**
	 * Returns true if this list contains no elements.
	 * 
	 * @return True if the list contains no elements, else false.
	 */
	public boolean isEmpty() {
		return this.hosts.isEmpty();
	}

}
